package table.tools;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NameValues {
	private List<NameValue> valueList;
	public NameValues(){
		this.valueList = new ArrayList<NameValue>();
	}
	
	public boolean hasName(String name){
		for(Iterator<NameValue> it = this.valueList.iterator(); it.hasNext();){
			NameValue nv = it.next();
			if(nv.name.equals(name)){
				return true;
			}
		}
		return false;
	}
	
	public Object getValueByName(String name){
		for(Iterator<NameValue> it = this.valueList.iterator(); it.hasNext();){
			NameValue nv = it.next();
			if(nv.name.equals(name)){
				return nv.value;
			}
		}
		return null;
	}
	
	public void setValueByName(String name, Object value){
		for(Iterator<NameValue> it = this.valueList.iterator(); it.hasNext();){
			NameValue nv = it.next();
			if(nv.name.equals(name)){
				nv.value = value;
				return ;
			}
		}
		NameValue newNameValue = new NameValue(name, value);
		this.valueList.add(newNameValue);
		return ;
	}

	public List<String> getNameList(){
		ArrayList<String> names = new ArrayList<String>();
		for(Iterator<NameValue> it = this.valueList.iterator(); it.hasNext();){
			NameValue nv = it.next();
			names.add(nv.name);
		}
		return names;
	}

	public void setValueList(List<NameValue> valueList){
		if(valueList != null && valueList.size() > 0){
			Iterator<NameValue> it = null;
			try{
				for(it = valueList.iterator(); it.hasNext();){
					NameValue namevalue = it.next();
					this.setValueByName(namevalue.name, namevalue.value);
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
}
